﻿using System.ComponentModel;
using RiskMan.Config;
using RiskMan.DataClass.Models.RM;
using System.Collections.ObjectModel;

namespace RiskMan.DataClass.Models
{
    /// <summary>
    /// описание содержания строки данных в таблице результата риск менеджера
    /// </summary>
    public class AccountLimiteItem : INotifyPropertyChanged
    {
        #region private 
        
        private string _account;
        private string _clientCode;
        private double _deposite;
        private double _currentBalans;
        //private double _block;
        private double _nextLevel;
        private double _profit;
        private double _comiss;
        private double _quantity;
        private double _full;
        private double _fullPersent;
        private ObservableCollection<InstrumentsAvaragePriceOpen> _instrumetnsPricOpen;
        private double _lastVm;
        private double _cumVm;
        private double _maxBalance;


        #endregion

        #region public

        /// <summary>
        /// Максимальное значение баланса за сессию
        /// </summary>
        public double MaxBalance
        {
            get { return _maxBalance; }
            set
            {
                if (_maxBalance != value)
                {
                    _maxBalance = value;
                    RaisePropertyChanged("MaxBalance");
                }
            }
        }

        /// <summary>
        /// для теста. Вывод расчетной вариационной маржи
        /// </summary>
        public double CumVM
        {
            get { return _cumVm; }
            set
            {
                if (_cumVm != value)
                {
                    _cumVm = value;
                    RaisePropertyChanged("CumVM");
                }
            }
        }

        /// <summary>
        /// Последнее изменение вариационной маржи
        /// </summary>
        public double LastVM
        {
            get { return _lastVm; }
            set
            {
                if (_lastVm != value)
                {
                    _lastVm = value;
                    RaisePropertyChanged("LastVM");
                }
            }
        }

        /// <summary>
        /// Цены приобретения инструментов
        /// </summary>
        public ObservableCollection<InstrumentsAvaragePriceOpen> InstrumetnsPricOpen
        {
            get { return _instrumetnsPricOpen; }
            set
            {

                if (_instrumetnsPricOpen != value)
                {
                    _instrumetnsPricOpen = value;
                    RaisePropertyChanged("InstrumetnsPricOpen");
                }
            }
        }

        public double FullPersent
        {
            get { return _fullPersent; }
            set
            {
                if ( _fullPersent != value )
                {
                    _fullPersent = value;
                    RaisePropertyChanged( "FullPersent" );
                }
            }
        }

        public string Account 
        {
            get { return _account; }
            set
            {
                if (_account != value)
                {
                    _account = value;
                    RaisePropertyChanged("Account");
                }
            }
        }
        public string ClientCode
        {
            get { return _clientCode; }
            set 
            {
                if (_clientCode != value)
                {
                    _clientCode = value;
                    RaisePropertyChanged("ClientCode");
                }
            }
        }
        public double Deposite
        {
            get { return _deposite; }
            set 
            {
                if (_deposite != value)
                {
                    _deposite = value;
                    RaisePropertyChanged("Deposite");
                }
            }
        }
        public double CurrentBalance
        {
            get { return _currentBalans; }
            set
            {
                if ( _currentBalans != value )
                {
                    _currentBalans = value;
                    RaisePropertyChanged("CurrentBalance");
                }
            }
        }
        //public double Block {
        //    get { return _block; }
        //    set
        //    {
        //        if (_block != value)
        //        {
        //            _block = value;
        //            RaisePropertyChanged("Block");
        //        }
        //    }
        //}
        public double NextLevel
        {
            get { return _nextLevel; }
            set
            {
                if (_nextLevel != value)
                {
                    _nextLevel = value;
                    RaisePropertyChanged("NextLevel");
                }
            }
        }
        public double Profit
        {
            get { return _profit; }
            set
            {
                if (_profit != value)
                {
                    _profit = value;
                    RaisePropertyChanged("Profit");
                }
            }
        }
        public double Comiss 
        {
            get { return _comiss; }
            set
            {
                if (_comiss != value)
                {
                    _comiss = value;
                    RaisePropertyChanged("Comiss");
                }
            }
        }
        public double Full
        {
            get { return _full; }
            set
            {
                if (_full != value)
                {
                    _full = value;
                    RaisePropertyChanged("Full");
                }
            }
        }
        public double Quantity
        {
            get { return _quantity; }
            set { _quantity = value; }
        }

        #endregion

        #region event PropertyChangedEventHandler

        public event PropertyChangedEventHandler PropertyChanged;
       

        protected void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        public double GetVarMarga ( RmItem rMItem, Data data )
        {
            for ( int i = 0; i < data.LimitebyAccount.Count; i++ )
            {
                if ( data.LimitebyAccount [ i ].TypeLimit != cfg.ZalogMoney )
                    if ( data.LimitebyAccount [ i ].Account == rMItem.Account )
                        return data.LimitebyAccount [ i ].VariableMarga;
            }
            return 0;
        }

        public double GetProfit(RmItem rMItem, Data data)
        {

            if ( rMItem.Account == rMItem.ClientCode )
            {
                if ( rMItem.Variable == cfg.Mode0 )
                {
                    for ( int i = 0; i < data.LimitebyAccount.Count; i++ )
                    {
                        if ( data.LimitebyAccount [ i ].TypeLimit != cfg.ZalogMoney )
                            if ( data.LimitebyAccount [ i ].Account == rMItem.Account )
                            {
                                //if ( rMItem.TypeRasch == cfg.PERSENT )
                                //    return ( data.LimitebyAccount [ i ].VariableMarga + data.LimitebyAccount [ i ].Dohod ) / result * 100;
                                //if ( rMItem.TypeRasch == cfg.RUB )
                                return ( data.LimitebyAccount [ i ].VariableMarga + data.LimitebyAccount [ i ].Dohod );
                            }
                    }
                }

                if ( rMItem.Variable == cfg.Mode1 )
                {
                    for ( int i = 0; i < data.LimitebyAccount.Count; i++ )
                    {
                        if ( data.LimitebyAccount [ i ].TypeLimit != cfg.ZalogMoney )
                            if ( data.LimitebyAccount [ i ].Account == rMItem.Account )
                            {
                                //if ( rMItem.TypeRasch == cfg.PERSENT )
                                //    return data.LimitebyAccount [ i ].VariableMarga / result * 100;
                                //if ( rMItem.TypeRasch == cfg.RUB )
                                return data.LimitebyAccount [ i ].VariableMarga;
                            }
                    }
                }
            }
            else
            {
                // ммвб
                //if ( rMItem.TypeRasch == TheControllerRisk.Settings.cfg.PERSENT )
                //{
                //    for ( int i = 0; i < data.Portfel.Count; i++ )
                //    {
                //        if ( data.Portfel [ i ].CodeClient == rMItem.ClientCode )
                //            return data.Portfel [ i ].PersentChange;
                //    }
                //}
                //else
                //{
                //if ( rMItem.TypeRasch == TheControllerRisk.Settings.cfg.RUB )
                //{
                for ( int i = 0; i < data.Portfel.Count; i++ )
                {
                    if ( data.Portfel [ i ].CodeClient == rMItem.ClientCode )
                        return data.Portfel [ i ].ProfitLoss;
                }
                //}
                //}
            }

            return 0;
        }

        public double GetDohod ( RmItem rMItem, Data data )
        {
            if ( rMItem.Account == rMItem.ClientCode )
            {

                for ( int i = 0; i < data.LimitebyAccount.Count; i++ )
                {
                    if ( data.LimitebyAccount [ i ].TypeLimit != cfg.ZalogMoney )
                        if ( data.LimitebyAccount [ i ].Account == rMItem.Account )
                            return data.LimitebyAccount [ i ].Dohod;
                    ;
                }
            }
            else
            {
                for ( int i = 0; i < data.Portfel.Count; i++ )
                {
                    if ( rMItem.ClientCode == data.Portfel [ i ].CodeClient )
                    {
                        return data.Portfel [ i ].ProfitLoss;
                    }
                }
            }

            return 0;
        }

        public double GetComiss ( RmItem rMItem, Data data )
        {
            int count = 0;

            if ( rMItem.Account == rMItem.ClientCode )
            {
                for ( int i = 0; i < data.FortsPositionsList.Count; i++ )
                {
                    if ( data.FortsPositionsList [ i ].Account == rMItem.Account && data.FortsPositionsList [ i ].Company.Substring( 0, 6 ) == "SPBFUT" )
                    {
                        count += data.FortsPositionsList [ i ].CurrentLongPos + data.FortsPositionsList [ i ].CurrentShortPos;
                    }
                }

                return count * RiskMan.Properties.Settings.Default.Commis;
            }
            else
            {
                // ммвб комиссия
                double result = 0;
                double volume = 0;
                for ( int i = 0; i < data.DealList.Count; i++ )
                {
                    if ( data.DealList [ i ].Account == rMItem.Account &&
                         data.DealList [ i ].ClientCode == rMItem.ClientCode )
                    {
                        volume += data.DealList [ i ].Volume;
                    }
                }
                for ( int i = 0; i < data.MmvbComisList.Count; i++ )
                {
                    if ( volume >= data.MmvbComisList [ i ].FromComis && volume < data.MmvbComisList [ i ].ToComis )
                    {
                        result = volume * data.MmvbComisList [ i ].Comis;
                        break;
                    }
                    else
                    {
                        if ( result == 0 && i == data.MmvbComisList.Count - 1 )
                            result = volume * data.MmvbComisList [ i ].Comis;
                    }
                }
                return result / 100;
            }
        }

        public double getQuantity(double block, double go)
        {
            return block / go;
        }

        
    }
}
 